$(function () {
    loadModuleInfo();
});

var zTreeObj;
function loadModuleInfo() {
    $.ajax({
        type: "post",
        //查询所有的资源列表时，传递角色ID，查询当前角色对应的已经授权的资源
        url: ctx + "/module/queryAllModules?roleId=" + $("input[name='roleId']").val(),
        dataType: "json",
        success: function (data) {
            // zTree 的参数配置
            var setting = {
                check: {
                    enable: true
                },
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onCheck: zTreeOnCheck
                }
            };
            zTreeObj = $.fn.zTree.init($("#test1"), setting, data);
        }
    });
}

function zTreeOnCheck(event, treeId, treeNode) {
    var nodes = zTreeObj.getCheckedNodes(true);
    if (nodes.length > 0) {
        // 获取所有资源的id值
        var mIds = "mIds=";
        for (var i = 0; i < nodes.length; i++) {
            if (i < nodes.length - 1) {
                mIds = mIds + nodes[i].id + "&mIds=";
            } else {
                mIds = mIds + nodes[i].id;
            }
        }
        console.log(mIds);
    }

    $.ajax({
        type: "post",
        url: ctx + "/role/addGrant",
        data: mIds + "&roleId=" + $("input[name='roleId']").val(),
        dataType: "json",
        success: function (data) {
            console.log(data);
        }
    });
}